package com.zz.ai.service;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.document.Document;
import org.springframework.ai.vectorstore.SearchRequest;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class ChatService {
    @Autowired
    private VectorStore vectorStore;

    @Autowired
    private ChatClient chatClient;

    public String generateResponse(String userQuery) {
        // 1. 检索相似文档
        List<Document> contextDocs = vectorStore.similaritySearch(
                SearchRequest.builder().query(userQuery).topK(5)
                        .similarityThreshold(0.7d).build()
        );

        // 2. 构建增强提示词
        String context = contextDocs.stream()
                .map(Document::getText)
                .collect(Collectors.joining("\n"));

        String prompt = String.format("基于以下上下文回答用户问题：\n%s\n问题：%s", context, userQuery);

//        .functions("doHotelUpdateArticle")
        // 3. 调用 AI 模型生成回答
        return chatClient.prompt()
                .user(prompt)
                .call()
                .content();
    }

    //1.回复不精确
    //2.需求明确
    //3.切片不明确 -- 精确度，切片大小
}
